home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xarchie-2.0.9 / view-file.c < prev    next >
C/C++ Source or Header  |  1995-06-18  |  8KB  |  284 lines

  1. /*
  2.  * view-file.c : Routines for the windows when files are "opened" via ftp
  3.  *
  4.  * George Ferguson, ferguson@cs.rochester.edu, 23 Apr 1993.
  5.  */
  6. #include <stdio.h>
  7. #include <X11/Intrinsic.h>
  8. #include <X11/Shell.h>
  9. #include <X11/StringDefs.h>
  10. #include <X11/Xaw/Form.h>
  11. #include <X11/Xaw/Label.h>
  12. #include <X11/Xaw/Command.h>
  13. #include <X11/Xaw/AsciiText.h>
  14. #include "config.h"
  15. #ifdef HAVE_SYS_PARAM_H
  16. #include <sys/param.h>
  17. #endif
  18. #include "xarchie.h"
  19. #include "fchooser.h"
  20. #include "stringdefs.h"
  21. #include "xutil.h"
  22. #include "syserr.h"
  23. #include "debug.h"
  24.  
  25. /*
  26.  * Functions defined here:
  27.  */
  28. void viewFile();
  29.  
  30. static void nonmaskableEventHandler();
  31. static void viewDone(),viewDown(),viewUp(),viewSave();
  32. static void viewSaveOk(),viewSaveCancel();
  33. static int fileCopy();
  34.  
  35. /*
  36.  * Data defined here:
  37.  */
  38. typedef struct _ViewFileInfo {
  39.     Widget shell;
  40.     Widget text;
  41.     String filename;
  42. } ViewFileInfo;
  43.  
  44. /*    -    -    -    -    -    -    -    -    */
  45.  
  46. void
  47. viewFile(filename)
  48. char *filename;
  49. {
  50.     ViewFileInfo *vfinfo;
  51.     Widget form,button;
  52.     Arg args[2];
  53.  
  54.     vfinfo = XtNew(ViewFileInfo);
  55.     vfinfo->filename = XtNewString(filename);
  56.     XtSetArg(args[0],XtNtitle,filename);
  57.     vfinfo->shell = XtCreatePopupShell("viewShell",topLevelShellWidgetClass,
  58.                        toplevel,args,1);
  59.     form = XtCreateManagedWidget("viewForm",formWidgetClass,
  60.                  vfinfo->shell,NULL,0);
  61.     button = XtCreateManagedWidget("viewDoneButton",commandWidgetClass,
  62.                    form,NULL,0);
  63.     XtAddCallback(button,XtNcallback,viewDone,(XtPointer)vfinfo);
  64.     button = XtCreateManagedWidget("viewDownButton",commandWidgetClass,
  65.                    form,NULL,0);
  66.     XtAddCallback(button,XtNcallback,viewDown,(XtPointer)vfinfo);
  67.     button = XtCreateManagedWidget("viewUpButton",commandWidgetClass,
  68.                    form,NULL,0);
  69.     XtAddCallback(button,XtNcallback,viewUp,(XtPointer)vfinfo);
  70.     button = XtCreateManagedWidget("viewSaveButton",commandWidgetClass,
  71.                    form,NULL,0);
  72.     XtAddCallback(button,XtNcallback,viewSave,(XtPointer)vfinfo);
  73.     XtSetArg(args[0],XtNtype,XawAsciiFile);
  74.     XtSetArg(args[1],XtNstring,filename);
  75.     vfinfo->text = XtCreateManagedWidget("viewText",asciiTextWidgetClass,
  76.                      form,args,2);
  77.     XtRealizeWidget(vfinfo->shell);
  78.     /* Allow WM_DELETE_WINDOW to the Shell to be Done */
  79.     (void)XSetWMProtocols(XtDisplay(vfinfo->shell),XtWindow(vfinfo->shell),
  80.               &WM_DELETE_WINDOW,1);
  81.     XtAddEventHandler(vfinfo->shell,NoEventMask,True,
  82.               nonmaskableEventHandler,(XtPointer)vfinfo);
  83.     XtPopup(vfinfo->shell,XtGrabNone);
  84. }
  85.  
  86. /*
  87.  * Nonmaskable event handler for Shell: If the event is a ClientMessage
  88.  * of WM_PROTOCOLS then act as if Done had been clicked.
  89.  */
  90. /*ARGSUSED*/
  91. static void
  92. nonmaskableEventHandler(w,client_data,event,continue_to_dispatch)
  93. Widget w;
  94. XtPointer client_data;
  95. XEvent *event;
  96. Boolean *continue_to_dispatch;
  97. {
  98.     DEBUG1("nonmaskableHandler: w=0x%x\n",w);
  99.     if (event->type == ClientMessage &&
  100.         event->xclient.data.l[0] == WM_DELETE_WINDOW) {
  101.     DEBUG0("nonmaskableHandler: calling cancelButtonCallback\n");
  102.     viewDone(NULL,client_data,NULL);
  103.     }
  104.     DEBUG0("nonmaskableHandler: done\n");
  105. }
  106.  
  107. /*    -    -    -    -    -    -    -    -    */
  108. /* Callbacks for the view window */
  109.  
  110. /*ARGSUSED*/
  111. static void
  112. viewDone(w,client_data,call_data)
  113. Widget w;
  114. XtPointer client_data;        /* ViewFileInfo */
  115. XtPointer call_data;        /* not used */
  116. {
  117.     ViewFileInfo *vfinfo = (ViewFileInfo *)client_data;
  118.     char *name;
  119.  
  120.     DEBUG0("viewDone...\n");
  121.     name = getWidgetString(vfinfo->text);
  122.     if (name != NULL && *name != '\0') {
  123.     DEBUG1("viewDone: unlinking \"%s\"\n",name);
  124.     if (unlink(name) < 0)
  125.         sysError(name);
  126.     }
  127.     XtPopdown(vfinfo->shell);
  128.     XtUnrealizeWidget(vfinfo->shell);
  129.     XtDestroyWidget(vfinfo->shell);
  130.     XtFree(vfinfo->filename);
  131.     XtFree((char*)vfinfo);
  132.     DEBUG0("viewDone: done\n");
  133. }
  134.  
  135. /*ARGSUSED*/
  136. static void
  137. viewDown(w,client_data,call_data)
  138. Widget w;
  139. XtPointer client_data;        /* ViewFileInfo */
  140. XtPointer call_data;        /* not used */
  141. {
  142.     ViewFileInfo *vfinfo = (ViewFileInfo *)client_data;
  143.  
  144.     XtCallActionProc(vfinfo->text,"next-page",NULL,NULL,0);
  145. }
  146.  
  147. /*ARGSUSED*/
  148. static void
  149. viewUp(w,client_data,call_data)
  150. Widget w;
  151. XtPointer client_data;        /* ViewFileInfo */
  152. XtPointer call_data;        /* not used */
  153. {
  154.     ViewFileInfo *vfinfo = (ViewFileInfo *)client_data;
  155.  
  156.     XtCallActionProc(vfinfo->text,"previous-page",NULL,NULL,0);
  157. }
  158.  
  159. /*ARGSUSED*/
  160. static void
  161. viewSave(w,client_data,call_data)
  162. Widget w;
  163. XtPointer client_data;        /* ViewFileInfo */
  164. XtPointer call_data;        /* not used */
  165. {
  166.     ViewFileInfo *vfinfo = (ViewFileInfo *)client_data;
  167.     FileChooserInfo *fcinfo;
  168.     Widget shell,form,text;
  169.     char *name,*basename;
  170.     Arg args[1];
  171.  
  172.     DEBUG0("viewSave...\n");
  173.     setBusyStatus(True);
  174.     shell = XtCreatePopupShell("viewSaveShell",topLevelShellWidgetClass,
  175.                    vfinfo->shell,NULL,0);
  176.     form = XtCreateManagedWidget("viewSaveForm",formWidgetClass,
  177.                  shell,NULL,0);
  178.     (void)XtCreateManagedWidget("viewSaveLabel",labelWidgetClass,
  179.                 form,NULL,0);
  180.     text = XtCreateManagedWidget("viewSaveLabelText",asciiTextWidgetClass,
  181.                  form,NULL,0);
  182.     name = vfinfo->filename;
  183.     setWidgetString(text,name);
  184.     fcinfo = createFileChooser(shell,form,"viewSave",viewSaveOk,
  185.                    viewSaveCancel,(XtPointer)vfinfo);
  186.     /* Adjust vertical layout */
  187.     XtSetArg(args[0],XtNfromVert,text);
  188. #ifdef FILECHOOSER
  189.     XtSetValues(fcinfo->fcw,args,1);
  190. #else
  191.     XtSetValues(fcinfo->text,args,1);
  192. #endif
  193.     /* Realize them all */
  194.     XtRealizeWidget(shell);
  195.     /* Set initial filename (has to be after realize for some reason) */
  196.     if ((basename=rindex(name,'/')) != NULL)
  197.     name = basename+1;
  198.     setWidgetString(fcinfo->text,name);
  199.     /* Register window for WM */
  200.     (void)XSetWMProtocols(XtDisplay(shell),XtWindow(shell),
  201.               &WM_DELETE_WINDOW,1);
  202.     /* Here we go */
  203.     XtPopup(shell,XtGrabNone);
  204.     setBusyStatus(False);
  205.     DEBUG0("viewSave: done\n");
  206. }
  207.  
  208. /*    -    -    -    -    -    -    -    -    */
  209. /* Callbacks from the view-save FileChooser */
  210.  
  211. /*ARGSUSED*/
  212. static void
  213. viewSaveOk(fcinfo,filename,client_data)
  214. FileChooserInfo *fcinfo;
  215. char *filename;
  216. XtPointer client_data;        /* ViewFileInfo */
  217. {
  218.     ViewFileInfo *vfinfo = (ViewFileInfo *)client_data;
  219.  
  220.     DEBUG1("viewSaveOk: fcinfo=0x%x\n",fcinfo);
  221.     DEBUG2("viewSaveOk: copying \"%s\" to \"%s\"\n",vfinfo->filename,filename);
  222.     if (fileCopy(vfinfo->filename,filename) >= 0) {
  223.     /* Remove the File Selector if successful */
  224.     XtPopdown(fcinfo->shell);
  225.     XtUnrealizeWidget(fcinfo->shell);
  226.     XtDestroyWidget(fcinfo->shell);
  227.     XtFree((char*)fcinfo);
  228.     }
  229.     DEBUG0("viewSaveOk: done\n");
  230. }
  231.  
  232. /*ARGSUSED*/
  233. static void
  234. viewSaveCancel(fcinfo,client_data)
  235. FileChooserInfo *fcinfo;
  236. XtPointer client_data;        /* ViewFileInfo */
  237. {
  238.     DEBUG1("viewSaveCancel: fcinfo=0x%x\n",fcinfo);
  239.     XtPopdown(fcinfo->shell);
  240.     XtUnrealizeWidget(fcinfo->shell);
  241.     XtDestroyWidget(fcinfo->shell);
  242.     XtFree((char*)fcinfo);
  243.     DEBUG0("viewSaveCancel: done\n");
  244. }
  245.  
  246. /*    -    -    -    -    -    -    -    -    */
  247. /* Misc. functions */
  248.  
  249. static int
  250. fileCopy(path1,path2)
  251. char *path1,*path2;
  252. {
  253.     FILE *infp,*outfp;
  254.     char buf[BUFSIZ];
  255.     int n,retcode;
  256.  
  257.     DEBUG2("fileCopy: \"%s\" \"%s\"\n",path1,path2);
  258.     if ((infp=fopen(path1,"r")) == NULL) {
  259.     sysError(path1);
  260.     return(-1);
  261.     }
  262.     if ((outfp=fopen(path2,"w")) == NULL) {
  263.     sysError(path2);
  264.     return(-1);
  265.     }
  266.     retcode = 0;
  267.     while (!feof(infp)) {
  268.     if ((n=fread(buf,1,sizeof(buf),infp)) <= 0) {
  269.         sysError("read");
  270.         retcode = -1;
  271.         break;
  272.     }
  273.     if (fwrite(buf,1,n,outfp) != n) {
  274.         sysError("write");
  275.         retcode = -1;
  276.         break;
  277.     }
  278.     }
  279.     fclose(infp);
  280.     fclose(outfp);
  281.     DEBUG0("fileCopy: done\n");
  282.     return(retcode);
  283. }
  284.